package com.mygdx.game.desktop.demo;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Align;

/**
 * 鼠标拖拽
 * 
 * @author Administrator
 *
 */
public class MouseDragTestGame extends Game {

	public SpriteBatch batch;

	private Stage stage;
	private Texture ballsTexture;
	private TextureRegion ballRegion;
	private Image ball;
	private float ballX;
	private float ballY;

	private Image ballB;

	private float cutPointX;
	private float cutPointY;
	
	private float rotationValue = 0.0f;
	@Override
	public void create() {

		batch = new SpriteBatch();

		stage = new Stage();

		ballsTexture = new Texture(Gdx.files.internal("images/ball.png"));
		// 取第一个球
		ballRegion = new TextureRegion(ballsTexture, 0, 0, ballsTexture.getWidth() / 4, ballsTexture.getHeight());
		ball = new Image(ballRegion);
		ball.setSize(120f, 120f);
		stage.addActor(ball);
		// 取第二个球
		ballRegion = new TextureRegion(ballsTexture, ballsTexture.getWidth() / 4, 0, ballsTexture.getWidth() / 4,
				ballsTexture.getHeight());
		ballB = new Image(ballRegion);
		ballB.setSize(120f, 120f);
		ballB.setPosition(stage.getWidth()/2 - ballB.getWidth()/2, stage.getHeight()/2 - ballB.getHeight()/2);
		stage.addActor(ballB);
	}

	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		stage.act();
		stage.draw();
		
		// 旋转
		ballB.setRotation(rotationValue += 3);
		// 是否触摸屏幕 或按下鼠标按钮
		if (Gdx.input.isTouched()) {
//			System.out.println("鼠标坐标：" + Gdx.input.getX() + "," + (stage.getHeight() - Gdx.input.getY()));
//			System.out.println("ball坐标：" + (ball.getImageX() + ball.getHeight()) + "," + (ball.getImageY() + ball.getWidth()));

			// 判断鼠标是否在 image范围内。 鼠标y值 = 舞台y值-鼠标原y值
			ballX = Gdx.input.getX();
			ballY = stage.getHeight() - Gdx.input.getY();
			if (ball.getX() < ballX && ballX < (ball.getX() + ball.getWidth()) && ball.getY() < ballY
					&& ballY < (ball.getY() + ball.getHeight()))
				ballDrag(ball, ballX, ballY);


//			if (ballB.getX() < ballX && ballX < (ballB.getX() + ballB.getWidth()) && ballB.getY() < ballY
//					&& ballY < (ballB.getY() + ballB.getHeight()))
//				ballDrag(ballB, ballX, ballY);


			// 碰撞判断
			if (ball.getX() + ball.getWidth() > ballB.getX() && ball.getX() < ballB.getX() +ballB.getWidth()
					&& ball.getY() + ball.getHeight() > ballB.getHeight() && ball.getY() < ballB.getY() + ballB.getHeight()) {
				System.out.println("触碰");
				cutPointX = Gdx.input.getX() - ball.getWidth()/2;
				cutPointY = stage.getHeight() - Gdx.input.getY() - ball.getHeight()/2;
				ball.setX(cutPointX);
				ball.setY(cutPointY);
			}
		}
	}

	public void dispose() {
		stage.dispose();
	}

	/**
	 * ball拖拽
	 * 
	 * @param x
	 * @param y
	 */
	public void ballDrag(Image image, float x, float y) {
		float positionX = x - image.getWidth() / 2;
		float positionY = y - image.getHeight() / 2;

		if (positionX < 0)
			positionX = 0f;
		if (positionX > stage.getWidth() - image.getWidth())
			positionX = stage.getWidth() - image.getWidth();
		if (positionY < 0)
			positionY = 0f;
		if (positionY > stage.getHeight() - image.getHeight())
			positionY = stage.getHeight() - image.getHeight();
		image.setPosition(positionX, positionY);
	}

	public static void main(String[] args) {
		new LwjglApplication(new MouseDragTestGame());
	}
}
